package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import net.sf.json.JSONObject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat

/**
 *
 * @Title: apiMsgPingJie* @Description:短信拼接
 * @table:
 * @author: TH* @date 2022-04-06
 *
 */
class apiMsgPingJie implements ServiceBackInvoke {
    private static final Logger log = LoggerFactory.getLogger(apiMsgPingJie.class);

    @Override
    String doCallBack(EliteBean eliteBean, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");

        JSONObject reJson = new JSONObject();

        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=UTF-8");

        // 调用IP过滤
        if (KHDBConfig.getParam(eliteBean, "#APIIP") == "1" && !KHUtil.getRemoteHost(httpServletRequest)) {
            log.warn("API被非法IP调用-Request Not Allow!");
            return "Request Not Allow!";
        }
        String Syncpram = "";

        String getMethod = httpServletRequest.getMethod();
        log.debug("getMethod:" + getMethod);
        if (getMethod.equals("GET")) {
            Syncpram = httpServletRequest.getQueryString();
            log.info("接收调用GET参数信息：" + Syncpram);
        } else if (getMethod.equals("POST")) {
            InputStreamReader reader = new InputStreamReader(httpServletRequest.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                Syncpram += line;
            }

        }
        log.info("接收调用POST报文信息：" + System.getProperty("line.separator") + Syncpram);

        try {

            JSONObject inJson = JSONObject.fromObject(Syncpram);

            String Sql1 = inJson.getString("sql1");//拼接的sql1
            String Sql2 = inJson.getString("sql2");//拼接的sql2
            String rolegroup_id = inJson.getString("rolegroup_id");
            String staff_id = inJson.getString("staff_id");
            String tempplate_id = inJson.getString("tempplate_id");
            String batch_guid = inJson.getString("poster_guid2");
            log.debug("DOCUMENTPATH字段处理SQL:" + Sql1 + " ---DOCUMENTPATH2字段处理SQL:" + Sql2);

            String updateSQL = '''INSERT INTO DOCUMENTOUT (
                                    DOCUMENTOUT_GUID,
                                    DOCTYPE,
                                    STATUS,
                                    TRIALCOUNT,
                                    TRIALCOUNTLIMIT,
                                    DOCUMENTPATH,
                                    DOCUMENTPATH2,
                                    HANDLEBY_ID,
                                    HANDLEGROUP_ID,
                                    CUSTOMER_GUID,
                                    CREATEDBY,
                                    CREATEDDATE,
                                    EXPECTEDSTARTDATE,
                                    DOCUMENTTO,
                                    LASTTRYDATETIME,
                                    sub_doctype,
                                    MSGID,
                                    TEMPLATE_ID,import_guid
                                    ) 
                                    SELECT
                                    rec_guid,
                                    3,
                                    0,
                                    0,
                                    3,
                                    ''' + Sql2 + ''', 
                                    concat('{',''' + Sql1 + ''','}'),
                                    \'''' + staff_id + '''\',
                                    \'''' + rolegroup_id + '''\',
                                    '',
                                    \'''' + staff_id + '''\',
                                    now(),
                                    now(),
                                    mobile,
                                    now(),
                                    '短信',
                                    concat(REPLACE ( unix_timestamp( CURRENT_TIMESTAMP ( 3 )), '.', '' ),CEILING(RAND()*900000+100000)),
                                    \'''' + tempplate_id + '''\',
                                    \'''' + batch_guid + '''\'
                                    FROM
                                    pa_importmsglist 
                                    WHERE
                                    batch_guid = \'''' + batch_guid + '''\'''';

            log.debug("拼接后的更新SQL:" + updateSQL);

            Integer insertNum = eliteBean.update(updateSQL, "dataSource");
            if (insertNum > 0) {
                log.debug("处理成功");
                reJson.put("code", 1);
                reJson.put("message", "处理批次数量：" + insertNum);
            } else {
                reJson.put("code", 0);
                reJson.put("message", "处理批次数量：" + insertNum);
            }
        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson.put("code", -1);
            reJson.put("message", "接口处理异常" + e.getMessage());
        }

        return reJson.toString();
    }


}
